/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;

import com.genia.toolbox.basics.i18n.SupportedLanguage;
import com.genia.toolbox.spring.provider.message.manager.CustomResourceBundleMessageSource;
import com.genia.toolbox.web.gwt.basics.client.bean.GwtLocale;
import com.genia.toolbox.web.gwt.basics.client.bean.impl.GwtLocaleImpl;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.service.I18nService;

/**
 * Implementation of {@link I18nService}.
 */
public class I18nServiceImpl
    implements I18nService
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(I18nServiceImpl.class);

  /**
   * the <code>MessageSource</code> of the application.
   */
  private transient CustomResourceBundleMessageSource messageSource;

  /**
   * cashing {@link GwtLocale} by their corresponding {@link Locale}.
   */
  private transient final Map<Locale, GwtLocale> gwtLocaleByLocale = new HashMap<Locale, GwtLocale>();



  /**
   * translate a <code>GwtI18nMessage</code> in the user locale.
   * 
   * @param message
   *          the message to translate
   * @return the translated message
   * @see com.genia.toolbox.web.gwt.basics.client.service.I18nService#getMessage(com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage)
   */
  private String getMessage(final GwtI18nMessage message)
  {
    return getMessageSource().getMessage(message.getKey(), message.getParameters(), LocaleContextHolder.getLocale());
  }



  /**
   * translate a <code>GwtI18nMessage</code> in the user locale.
   * 
   * @param messages
   *          the messages to translate
   * @return the translated message
   * @see com.genia.toolbox.web.gwt.basics.client.service.I18nService#getMessage(com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage)
   */
  public List<String> getMessage(final List<GwtI18nMessage> messages)
  {
    List<String> res = new ArrayList<String>();
    for (Object object : messages) {
      GwtI18nMessage message = (GwtI18nMessage) object;
      try {
        res.add(getMessage(message));
      }
      catch (Exception e) {
        LOGGER.warn(e.getMessage(), e);
        res.add(null);
      }
    }
    return res;
  }



  /**
   * getter for the messageSource property.
   * 
   * @return the messageSource
   */
  public CustomResourceBundleMessageSource getMessageSource()
  {
    return messageSource;
  }



  /**
   * setter for the messageSource property.
   * 
   * @param messageSource
   *          the messageSource to set
   */
  public void setMessageSource(final CustomResourceBundleMessageSource messageSource)
  {
    this.messageSource = messageSource;
  }



  /**
   * returns the {@link GwtLocale} associated to a {@link Locale}.
   * 
   * @param locale
   *          the {@link Locale} of the {@link GwtLocale}
   * @return the {@link GwtLocale} associated to a {@link Locale}
   */
  private GwtLocale getGwtLocaleForLocale(Locale locale)
  {
    GwtLocale gwtLocale = gwtLocaleByLocale.get(locale);
    if (gwtLocale == null) {
      GwtLocaleImpl gwtLocaleImpl = new GwtLocaleImpl();
      gwtLocaleImpl.setStringRepresentation(locale.toString());

      String displayLanguage = locale.getDisplayLanguage(locale);
      if (!locale.getCountry().equals("")) {
        String displayCountry = locale.getDisplayCountry(locale);
        if (!locale.getVariant().equals("")) {
          displayCountry = displayCountry + " - " + locale.getDisplayVariant(locale);
        }
        displayLanguage = displayLanguage + " (" + displayCountry + ")";
      }
      gwtLocaleImpl.setDisplayLanguage(displayLanguage);
      gwtLocale = gwtLocaleImpl;
      gwtLocaleByLocale.put(locale, gwtLocale);
    }
    return gwtLocale;
  }



  /**
   * returns the list of registered locales for the application.
   * 
   * @return the list of registered locales for the application
   * @see com.genia.toolbox.web.gwt.basics.client.service.I18nService#getRegisteredLocale()
   */
  public GwtLocale[] getRegisteredLocale()
  {
    GwtLocale[] res = new GwtLocale[getMessageSource().getSupportedLanguages().size()];
    int index = 0;
    for (SupportedLanguage supportedLanguage : getMessageSource().getSupportedLanguages()) {
      res[index] = getGwtLocaleForLocale(supportedLanguage.getLocale());
      index++;
    }
    return res;
  }

}
